/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.pig4cloud.pigx.ruralLibrary.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pig4cloud.pigx.common.core.util.R;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookDataTypeEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookExposureEntity;
import com.pig4cloud.pigx.ruralLibrary.api.entity.BookTypeEntity;
import com.pig4cloud.pigx.ruralLibrary.mapper.BookTypeMapper;
import com.pig4cloud.pigx.ruralLibrary.service.BookTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 中图法分类表
 *
 * @author pig
 * @date 2023-09-26 09:27:21
 */
@Service
public class BookTypeServiceImpl extends ServiceImpl<BookTypeMapper, BookTypeEntity> implements BookTypeService {


    @Autowired
    private BookDataTypeServiceImpl bookDataTypeService;

    @Value("${digitallibrary.httpIp}")
    private String httpIp;

    @Value("${digitallibrary.username}")
    private String username;

    @Value("${digitallibrary.password}")
    private String password;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public R synchronization() {
        try {
            String token = bookDataTypeService.selectToken(httpIp + "/auth/oauth2/token");
            ObjectMapper objectMapper = new ObjectMapper();
            BookExposureEntity bookExposureEntity = new BookExposureEntity();
            bookExposureEntity.setUsername(username);
            bookExposureEntity.setPassword(password);
            String json = objectMapper.writeValueAsString(bookExposureEntity);
            String jsonString =bookDataTypeService.resourceType(token, httpIp + "/digitallibrary/bookExposure/getBookType", json);
            JSONObject jsonObject = JSON.parseObject(jsonString);
            JSONArray dataArray = jsonObject.getJSONArray("data");
            LambdaQueryWrapper<BookTypeEntity> wrapper = Wrappers.lambdaQuery();
            wrapper.eq(BookTypeEntity::getType,"0");
            this.remove(wrapper);
            for (int i = 0; i < dataArray.size(); i++) {
                JSONObject dataObject = dataArray.getJSONObject(i);
                BookTypeEntity bookType = new BookTypeEntity();
                String id = dataObject.getString("id");
                bookType.setId(Long.valueOf(id));
                bookType.setBookType(dataObject.getString("bookType"));
                bookType.setTypeName(dataObject.getString("typeName"));
                bookType.setDataTypeStatus(dataObject.getString("dataTypeStatus"));
                String id1 = dataObject.getString("datatypeId");
                bookType.setDatatypeId(Long.valueOf(id1));
                bookType.setType("0");
                this.save(bookType);
            }
            return R.ok("同步成功");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

}
